package cn.zyjblogs.controller;

import cn.zyjblogs.Customer;
import cn.zyjblogs.client.OtherServiceClient;
import cn.zyjblogs.client.SearchClient;
import cn.zyjblogs.service.CustomerService;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RefreshScope
public class CustomerController {

  @Value("${env}")
  private String env;



  @Value("${version}")
  private String version;

  @Autowired
  RestTemplate restTemplate;

  @Autowired
  private EurekaClient eurekaClient;

  @Autowired
  private SearchClient searchClient;

  @Autowired
  private CustomerService customerService;

  @Autowired
  private OtherServiceClient otherServiceClient;


  @GetMapping("/env")
  public String env() {
    return env;
  }


  @GetMapping("/version")
  public String version() throws InterruptedException {
    Thread.sleep(3000);
    return version;
  }

  @GetMapping("/list")
  public String list() {
    return otherServiceClient.list();
  }

  @GetMapping("/customer")
  public String customer() {
    System.out.println(Thread.currentThread().getName());
    /*
        //直接使用EurekaClient调用的方式
         //1. 通过eurekaClient 获取到SEARCH服务的信息
         InstanceInfo info = eurekaClient.getNextServerFromEureka("SEARCH", false);
         //2. 获取到访问的地址
         String url = info.getHomePageUrl();
         System.out.println(url);
         //3. 通过restTemplate访问
         String result = restTemplate.getForObject(url + "/search", String.class);
    */

    /*
        //Robbin时使用的调用方式
        String result = restTemplate.getForObject("http://SEARCH/search", String.class);
    */
    String result = searchClient.search();
    // 4. 返回
    return result;
  }

  @GetMapping("/customer/{id}")
  @HystrixCommand(
      fallbackMethod = "findByIdFallBack",
      commandProperties = {
        /*@HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),*/
        @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "70"),
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),
      })
  public Customer findById(@PathVariable Integer id) throws InterruptedException {
    System.out.println(Thread.currentThread().getName());
    //        Thread.sleep(3000);
    if (id == 1) {
      int i = 1 / 0;
    }
    System.out.println(customerService.findById(id));
    System.out.println(customerService.findById(id));
    customerService.clearFindById(id);
    System.out.println(customerService.findById(id));
    System.out.println(customerService.findById(id));
    customerService.clearFindById(id);
    return searchClient.findById(id);
  }

  // findById的降级方法，方法描述要与接口一致
  public Customer findByIdFallBack(Integer id) {
    return new Customer(-1, "", 0);
  }

  @GetMapping("/getCustomer")
  public Customer getcustomer(@RequestParam Integer id, @RequestParam String name) {
    return searchClient.getcustomer(id, name);
  }

  @GetMapping("save")
  public Customer save(Customer customer) {
    return searchClient.save(customer);
  }
}
